import {Component, OnInit, ViewChild} from "@angular/core";
import {DevService, PowerService} from "../../energy/api";
import {NbDateService, NbDialogService} from "@nebular/theme";
import {ActivatedRoute} from "@angular/router";
import {DataDataSource} from "../ele-history/ele-history.component";
import {BaseRequest} from "../../utils/BaseRequest";
import {ElectricityConsumptionData} from "../../energy/api/model/electricityConsumptionData";
import {PowerExtraService} from "../../energy/api/api/powerExtra.service";

@Component({
  templateUrl:'./device-detail.component.html',
})
export class DeviceDetailComponent implements  OnInit {
  settings = {
    pager: {
      display: true,
      perPage: 20
    },
    hideSubHeader: true,
    actions: {
      columnTitle: '操作',
      add: false,
      delete: false,
      edit: false,
      position: 'right',
    },
    columns: {
      devNo: {
        title: '设备编号',
        type: 'string',
      },
      isPooled: {
        title: '是否公摊',
        type: 'string',
      },
      dataStartTime: {
        title: '起始时间',
        type: 'string',
      },
      dataEndTime: {
        title: '结束时间',
        type: 'string',
      },
      beginMeterReading: {
        title: '起始表值',
        type: 'number',
      },
      endMeterReading: {
        title: '结束表值',
        type: 'number',
      },
      totalConsumption: {
        title: '总计消耗',
        type: 'number',
      },
      phaseCurrentA: {
        title: 'A的相电流',
        type: 'number',
      },
      phaseCurrentB: {
        title: 'B的相电流',
        type: 'number',
      },
      phaseCurrentC: {
        title: 'C的相电流',
        type: 'number',
      },
      phaseVoltageA: {
        title: 'A的相电压',
        type: 'number',
      },
      phaseVoltageB: {
        title: 'B的相电压',
        type: 'number',
      },
      phaseVoltageC: {
        title: 'C的相电压',
        type: 'number',
      },
      phaseActivePowerA: {
        title: 'A的相有功功率',
        type: 'number',
      },
      phaseActivePowerB: {
        title: 'B的相有功功率',
        type: 'number',
      },
      phaseActivePowerC: {
        title: 'C的相有功功率',
        type: 'number',
      },
      phaseReactivePowerA: {
        title: 'A的相无功功率',
        type: 'number',
      },
      phaseReactivePowerB: {
        title: 'B的相无功功率',
        type: 'number',
      },
      phaseReactivePowerC: {
        title: 'C的相无功功率',
        type: 'number',
      },
      powerFactor: {
        title: '功率因数',
        type: 'number',
      },
      totalPositiveMeterReading: {
        title: '正总表码',
        type: 'number',
      },
      tipPositiveMeterReading: {
        title: '正尖表码',
        type: 'number',
      },
      peakPositiveMeterReading: {
        title: '正峰表码',
        type: 'number',
      },
      plainPositiveMeterReading: {
        title: '正平表码',
        type: 'number',
      },
      valleyPositiveMeterReading: {
        title: '正谷表码',
        type: 'number',
      },
      totalReverseMeterReading: {
        title: '负总表码',
        type: 'number',
      },
      tipReverseMeterReading: {
        title: '负尖表码',
        type: 'number',
      },
      peakReverseMeterReading: {
        title: '负峰表码',
        type: 'number',
      },
      plainReverseMeterReading: {
        title: '负平表码',
      },
      valleyReverseMeterReading: {
        title: '负谷表码',
        type: 'number',
      },
      totalPositiveActivePower: {
        title: '总正向有功',
        type: 'number',
      },
      tipPositiveActivePower: {
        title: '正向有功尖',
        type: 'number',
      },
      peakPositiveActivePower: {
        title: '正向有功峰',
        type: 'number',
      },
      plainPositiveActivePower: {
        title: '正向有功平',
        type: 'number',
      },
      valleyPositiveActivePower: {
        title: '正向有功谷',
        type: 'number',
      },
      totalReverseActivePower: {
        title: '反向有功总',
        type: 'number',
      },
      totalReverseReactivePower: {
        title: '反向无功总',
        type: 'number',
      },
      rate: {
        title: '倍率',
        type: 'number',
      },
      extInfo: {
        title: '额外信息',
        type: 'string',
      },
    }
  };
  @ViewChild("dialog") devCtrlDialog;
  devNo: string = '';
  startTime: Date = new Date();
  endTime: Date = new Date();
  source:DataDataSource;// = new DataDataSource();
  ipAddress:string = '';
  macAddress:string = '';
  devName:string = '';
  optionPhaseCurrent = {};
  optionPhaseVoltage = {};
  optionPhaseActivePower = {};
  optionPhaseReactivePower = {};
  constructor(private powerApi:PowerService
              ,private powerExtraApi:PowerExtraService
              ,private devApi:DevService
              , private route: ActivatedRoute
              ,private dialog:NbDialogService
              , private dateService: NbDateService<Date>){
    this.startTime.setDate(0);
  }
  ngOnInit() {
    this.route.paramMap.subscribe(value=>{
      const devNo = value.get("devNo");
      if (devNo != null) this.devNo = devNo;
      const query = {
        pageIndex: 0,
        pageSize: 1,
        devNo:this.devNo
      };
      this.devApi.postECAGetDeviceInfo(new BaseRequest(query)).subscribe(response=>{
        this.ipAddress = response.data[0].devIp;
        this.macAddress = response.data[0].devMac;
        this.devName = response.data[0].devName;
      });
      this.source = new DataDataSource(this.powerApi,this.dateService,this.devNo,this.startTime,this.endTime,(data)=>{
        this.phaseCurrent(data);
        this.phaseVoltage(data);
        this.phaseActivePower(data);
        this.phaseReactivePower(data);
      });
    });
    this.powerExtraApi.requestDailyElectrcConsumptionData(new BaseRequest({
      pageIndex:0,
      pageSize:1,
      startTime:this.dateService.format(this.dateService.addDay(this.dateService.today(),-1),"yyyyMMdd"),
      endTime:this.dateService.format(this.dateService.today(),"yyyyMMdd"),
      unitId:this.devNo
    })).subscribe(res=>{
      res.data.forEach(value=>{

      });
    });
  }

  phaseCurrent(value:Array<ElectricityConsumptionData>){
      const dataA = value.map(v=>{
        return [new Date(v.dataStartTime).getTime(),v.phaseCurrentA];
      });
      const dataB = value.map(v=>{
        return [new Date(v.dataStartTime).getTime(),v.phaseCurrentB];
      });
      const dataC = value.map(v=>{
        return [new Date(v.dataStartTime).getTime(),v.phaseCurrentC];
      });
      this.optionPhaseCurrent = {
        xAxis: {
          type: 'time',
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: dataA,
          type: 'line',
          areaStyle: {}
        },{
          data: dataB,
          type: 'line',
          areaStyle: {}
        },{
          data: dataC,
          type: 'line',
          areaStyle: {}
        }]
      };
  }

  phaseVoltage(value:Array<ElectricityConsumptionData>){
    const dataA = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseVoltageA];
    });
    const dataB = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseVoltageB];
    });
    const dataC = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseVoltageC];
    });
    this.optionPhaseVoltage = {
      xAxis: {
        type: 'time',
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        data: dataA,
        type: 'line',
        areaStyle: {}
      },{
        data: dataB,
        type: 'line',
        areaStyle: {}
      },{
        data: dataC,
        type: 'line',
        areaStyle: {}
      }]
    };
  }
  phaseActivePower(value:Array<ElectricityConsumptionData>){
    const dataA = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseActivePowerA];
    });
    const dataB = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseActivePowerB];
    });
    const dataC = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseActivePowerC];
    });
    this.optionPhaseActivePower = {
      xAxis: {
        type: 'time',
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        data: dataA,
        type: 'line',
        areaStyle: {}
      },{
        data: dataB,
        type: 'line',
        areaStyle: {}
      },{
        data: dataC,
        type: 'line',
        areaStyle: {}
      }]
    };
  }
  phaseReactivePower(value:Array<ElectricityConsumptionData>){
    const dataA = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseReactivePowerA];
    });
    const dataB = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseReactivePowerB];
    });
    const dataC = value.map(v=>{
      return [new Date(v.dataStartTime).getTime(),v.phaseReactivePowerC];
    });
    this.optionPhaseReactivePower = {
      xAxis: {
        type: 'time',
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        data: dataA,
        type: 'line',
        areaStyle: {}
      },{
        data: dataB,
        type: 'line',
        areaStyle: {}
      },{
        data: dataC,
        type: 'line',
        areaStyle: {}
      }]
    };
  }
  onCtrl(){
    this.dialog.open(this.devCtrlDialog).onClose.subscribe(v=>{
      if(v!=null)
        alert("发送失败，设备尚未连接")
    })
  }
}

